<?php

namespace app\controller\admin\myworld;


use app\model\api\WorldMagic;
use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Good
 * @package app\controller\admin\myworld
 */
class Magic extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法
    public $orderRule = ['id' => 'desc'];

    public function _initialize()
    {
        $this->model = new WorldMagic();
    }

    //添加
    public function add()
    {
        $post     = CommonFun::filterPostData($this->request->post());

        $insData['name'] = $post['name'];
        $insData['img']  = $post['img'];
        $insData['effect']     = $post['effect'];
        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    //编辑
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        $row->name    = $post['name'];
        $row->img     = $post['img'];
        $row->effect         = $post['effect'];

        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function info(){
        $id = $this->request->param('id');
        $magic = $this->model->where('id',$id)->find()->toArray();
        $magic['name']   = !empty($magic['name'])?$magic['name']:'';
        $magic['effect']   = !empty($magic['effect'])?$magic['effect']:'';
        return $this->success('获取成功', $magic);
    }
}